<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="100px">
      <el-form-item label="送审给" prop="verifyInfoStr">
        <el-row>
          <el-col :span="22">
            <el-input v-model="this.verifyInfoStr" :disabled="true"></el-input>
          </el-col>

          <el-col :span="2" class="icon-list__tips">
            <el-tooltip placement="top" effect="light">
              <div slot="content">送审角色来源于审批流程配置，如果送审角色为空，请向系统管理员进行反馈</div>
              <i class="el-icon-warning"></i>
            </el-tooltip>
          </el-col>
        </el-row>
      </el-form-item>

      <el-form-item label="请假时间" prop="duringTime">
        <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="dataForm.beginTime" type="datetime"
          placeholder="请选择开始时间" :picker-options="pickerBeginOption"></el-date-picker>
        <span> 至 </span>
        <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="dataForm.endTime" type="datetime"
          placeholder="请选择结束时间" :picker-options="pickerBeginOption"></el-date-picker>
      </el-form-item>

      <el-form-item label="备注" prop="resignationNote">
        <el-input type="textarea" maxlength="200" show-word-limit :autosize="{ minRows:2, maxRows: 6}"
          v-model="dataForm.resignationNote"></el-input>
      </el-form-item>

      <el-form-item label="附件" prop="file">
        <single-upload v-model="dataForm.fileUrl" ref="singleUpload" @change="costPlannedAmountChange"></single-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import SingleUpload from '@/components/upload/singleUpload'
  export default {
    data() {
      var validateVerifyInfoStr = (rule, value, callback) => {
        if (this.dataForm.verifyDeptId === undefined || this.dataForm.verifyRoleId === undefined) {
          callback(new Error('送审人不能为空'))
        } else {
          callback()
        }
      }
      var validateDuringTime = (rule, value, callback) => {
        if (this.dataForm.beginTime === undefined || this.dataForm.endTime === undefined) {
          callback(new Error('请假时间不能为空'))
        } else if (this.dataForm.beginTime > this.dataForm.endTime) {
          callback(new Error('开始时间不能晚于结束时间'))
        } else {
          callback()
        }
      }
      var validateFile = (rule, value, callback) => {
        if (this.dataForm.fileUrl === undefined || this.dataForm.fileName === undefined) {
          callback(new Error('附件不能为空'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        verifyInfoStr: undefined,
        dataForm: {
          id: 0,
          resignationNote: undefined,
          fileUrl: undefined,
          fileName: undefined,
          verifyDeptId: undefined,
          verifyRoleId: undefined,
          nextVerifyId: undefined,
          beginTime: undefined,
          endTime: undefined
        },
        dataRule: {
          verifyInfoStr: [
            { required: true, validator: validateVerifyInfoStr, trigger: ['change', 'blur'] }
          ],
          duringTime: [
            { required: true, validator: validateDuringTime, trigger: ['change', 'blur'] }
          ],
          file: [
            { required: true, validator: validateFile, trigger: 'blur' }
          ]
        },
        pickerBeginOption: {
          disabledDate: (time) => {
            return time.getTime() < Date.now() - 1 * 24 * 60 * 60 * 1000
          }
        }
      }
    },
    components: {
      SingleUpload
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.dataForm.fileName = undefined
        this.dataForm.fileUrl = undefined
        this.dataForm.beginTime = undefined
        this.dataForm.endTime = undefined
        this.dataForm.resignationNote = undefined
        this.$nextTick(() => {
          this.$refs.singleUpload.reset()
        })
        this.visible = true
        this.$http({
          url: this.$http.adornUrl(`/sys/user/getDeptManager`),
          method: 'post',
          data: 5
        }).then(({ data }) => {
          this.dataForm.verifyDeptId = data.approvalDeptId
          this.dataForm.verifyRoleId = data.approvalRoleId
          this.dataForm.nextVerifyId = data.nextApprovalId
          this.verifyInfoStr = data.approvalDeptStr + '  ' + data.approvalRoleStr
          if (data && data.code === 500) {
            this.$message.error(data.msg)
            this.visible = false
          }

          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/examineapprove/my-resignation/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.dataForm.beginTime = data.resignation.beginTime
                this.dataForm.endTime = data.resignation.endTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          console.log(this.dataForm)
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/examineapprove/my-resignation/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'beginTime': this.dataForm.beginTime,
                'endTime': this.dataForm.endTime,
                'resignationNote': this.dataForm.resignationNote,
                'verifyDeptId': this.dataForm.verifyDeptId,
                'verifyRoleId': this.dataForm.verifyRoleId,
                'nextVerifyId': this.dataForm.nextVerifyId,
                'fileUrl': this.dataForm.fileUrl,
                'fileName': this.dataForm.fileName,
                'verifyStyle': 5
              })
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      async costPlannedAmountChange(url, name) {
        this.dataForm.fileUrl = url
        this.dataForm.fileName = name
      }
    }
  }
</script>

<style lang="scss">
  .icon-list__tips {
    font-size: 18px;
    text-align: center;
    color: #e6a23c;
    cursor: pointer;
  }
</style>